<?php
if(isset($_POST['gpid']) && isset($_POST['guid']))
{
	require("includes/basic.php");
	$uid=$_POST['guid'];	
	$d=$_POST['gpid'];
	$id= split(',',$d);
	foreach($id as $a => $b)
	{
		$assign=mysql_query("insert into task_assigned_to (`task_assigned_to`,`task_assigned_on`,`task_assigned_by`,`project_id`,`role_id`) values('$uid',sysdate(),'$userId','$b','1')");
	}
	
	
}
if(isset($_POST['gpid']) && isset($_POST['gvid']))
{
	require("includes/basic.php");
	$vid=$_POST['gvid'];	
	$d=$_POST['gpid'];
	$id= split(',',$d);
	foreach($id as $a => $b)
	{
		$assign=mysql_query("insert into task_assigned_to (`task_assigned_to`,`task_assigned_on`,`task_assigned_by`,`project_id`,`role_id`) values('$vid',sysdate(),'$userId','$b','1')");
		$venpro=mysql_query("insert into vendor_projects (`vendor_id`,`project_id`,`assigned_on`) values ('$vid','$b',sysdate())");
	}
	
	
}
if(isset($_POST['apid']))
{
	require("includes/basic.php");
	$d=$_POST['apid'];
	$id= split(',',$d);
	foreach($id as $a => $b)
	{
		$activatep=mysql_query("update project_master set record_status='Y' where project_id=$b");
	}	
	
}
if(isset($_POST['dpid']))
{
	require("includes/basic.php");
	$d=$_POST['dpid'];
	$id= split(',',$d);
	foreach($id as $a => $b)
	{
		$deactivatep=mysql_query("update project_master set record_status='N' where project_id=$b");
	}	
}
if(isset($_POST['s']))
{
	require("includes/basic.php");
	$s=$_POST['s'];	
	if(isset($_SESSION['uid']) && $userType=='O')
	{
		$sql="select * from project_master where project_created_by=$customerid and LOWER(project_name) like LOWER('$s%')";
		
	}
	elseif(isset($_SESSION['uid']) && $userType=='V')
	{	
		$sql="select * from project_master pm, vendor_projects vp where pm.project_created_by=$customerid and pm.project_id=vp.project_id and vp.vendor_id=$userId";
	}
	elseif(isset($_SESSION['uid']) && $userType=='D')
	{
		$sql="select * from project_master p, task_assigned_to t where p.project_created_by=$customerid and p.project_id=t.project_id and t.task_assigned_to=$userId and LOWER(p.project_name) like LOWER('$s%')";
		
	}
	
}
else
{
	if(isset($_SESSION['uid']) && $userType=='O')
	{
		$sql="select * from project_master where project_created_by=$customerid";
	}
	elseif(isset($_SESSION['uid']) && $userType=='V')
	{	
		$sql="select * from project_master pm, vendor_projects vp where pm.project_created_by=$customerid and pm.project_id=vp.project_id and vp.vendor_id=$userId";
	}
	elseif(isset($_SESSION['uid']) && $userType=='D')
	{
		$sql="select DISTINCT (t.project_id), p . * from project_master p, task_assigned_to t where p.project_created_by=$customerid and p.project_id=t.project_id and t.task_assigned_to=$userId";
		
	}
	
}
require('ps_pagination.php');  
$pager = new PS_Pagination($conn, $sql, 8, 5, "param1=valu1&param2=value2");
	
	/*
	 * Enable debugging if you want o view query errors
	*/
	$pager->setDebug(true);
	
	/*
	 * The paginate() function returns a mysql result set
	 * or false if no rows are returned by the query
	*/
	$rs = $pager->paginate();
	if(!$rs) die(mysql_error());


	$sr=1;
$class="evenrowbg";
?>

<form id='viewprojects' name='viewprojects' action='' method='post'>

<table width='100%' cellpadding='1' cellspacing='0' class='list'>
	<tr>
		<th width='5%' style='white-space: nowrap;'>
			<div style='display:block;'>
				<span class="link linkdisabled"><?php if(isset($_SESSION['uid']) && $userType=='O') { echo "P"; } else { $sr=1; echo "Sr. no."; } ?></span>
			</div>
		</th>
		<th width='4%' style='white-space: nowrap;'>
			<div style='display:block;'>
				<span class="link linkdisabled">Open</span>
			</div>
		</th>
		<th width='20%' style='white-space: nowrap;'>
			<div style='display:block;'>
				<span class="link">Project</span>
			</div>
		</th>
		<th width='35%' style='white-space: nowrap;'>
			<div style='display:block;'>
				<span class="link linkdisabled">Project Description</span>
			</div>
		</th>
		<th width='17%' style='white-space: nowrap;' align='center'>
			<span class="link">Project Manager</span>
		</th>
		<th width='15%' style='white-space: nowrap;'>
			<span class="link">Client</span>
		</th>
		<th width='4%' style='white-space: nowrap;'>
			<span class="link">Status</span>
		</th>
		<?php
		if(isset($_SESSION['uid']) && $userType=='O')
	{  ?>
		<th width='0' class='select'>
			<input type='checkbox' class='checkbox' id='listSelectAll' name='listSelectAll'>
		</th>
		<?php
	}
	?>
	</tr>
	<?php while($projectr=mysql_fetch_array($rs))
	{
		
		$projectid=$projectr['project_id'];
		$proname=$projectr['project_name'];
		$prodesc=substr($projectr['project_description'],0,50);
		$prostatus=$projectr['record_status'];
		if($class=="evenrowbg")
		{
			$class="oddrowbg";
		}
		else
		{
			$class="evenrowbg";
		}
		echo "<tr class='$class'><td style='text-align:center;'>";
		if($userType=='O')
		{
		echo "<a href='edit-project.php?p=$projectid'><img src='images/edit.png' alt='Edit' title='Edit'></a>";
		}
		else
		{
		echo $sr;
		}
		echo "</td>";
		echo "<td style='text-align:center;'><a href='open-project.php?p=$projectid'><img src='images/open-project.png' alt='Open Project' title='Open Project'></a></td>";
		echo "<td>".$proname."</td><td>".$prodesc."...</td>";
		echo "<td>";
		$resource=mysql_query("select * from task_assigned_to t, user_master u where t.task_assigned_to=u.user_id and t.project_id=".$projectid." and t.role_id=1");
		if(mysql_num_rows($resource))
			{
				while($resr=mysql_fetch_array($resource))
					{
						$pm=$resr['first_name']." ".$resr['last_name'];
						echo "$pm<br/>";
					}
			}
		else {echo "-";}
		echo "</td><td>";
		$client=mysql_query("select c.* from client_master c, client_project cp where c.client_id=cp.client_id and cp.project_id=$projectid");
		if(mysql_num_rows($client)>0)
		{
			while($clientr=mysql_fetch_array($client))
			{
			extract($clientr);
			$clientname=$clientr['client_name'];
			echo $clientname."<br/>";
			}
		}
		echo "</td><td style='text-align:center;'>";		
		if($prostatus=='Y')
		{
			echo "<img src='images/multi_activate.png' alt='Active' title='Active'>";
		}
		else
		{
			echo "<img src='images/disabled.png' alt='Disabled' title='Disabled'>";
		}
		echo "</td>";
		if(isset($_SESSION['uid']) && $userType=='O')
		{  
		echo "<td><input type='checkbox' value='$projectid' id='chkName' name='chkName'></td>";
		}
		$sr++;
	}
	?>
</table>
</form>

<?php

	//Display the full navigation in one go
	 echo "<br />\n";?>
	